/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.bice.backbean;

import com.bice.controller.entity.BiceUser;
import com.bice.controller.service.BiceUserService;
import com.bice.util.annotation.SessionResource;
import com.bice.util.annotation.SpringServiceResource;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;

/**
 *
 * @author Sirius
 */
@ManagedBean
@ViewScoped
public class IndexBean extends BackBean {

    private String nickName;
    private String password;
    private boolean errorLogin;
    
    @SpringServiceResource
    private transient BiceUserService biceUserService;
    @SessionResource
    private SessionBean session;
    
    /**
     * Creates a new instance of IndexBean
     */
    public IndexBean() {
        nickName = "";
        password = "";
        
    }

    /**
     * @return the nickName
     */
    public String getNickName() {
        return nickName;
    }

    /**
     * @return the password
     */
    public String getPassword() {
        return password;
    }

    /**
     * @return the errorLogin
     */
    public boolean getErrorLogin() {
        return errorLogin;
    }
    
    public void login(){
        errorLogin = false;
        BiceUser current = biceUserService.getUser(nickName, encrypt(password));
        if(current != null) {
            session.setUser(current);
        } else {
            errorLogin = true;
        }
    }
    
    /**
     * @param nickName the nickName to set
     */
    public void setNickName(String nickName) {
        this.nickName = nickName;
    }

    /**
     * @param password the password to set
     */
    public void setPassword(String password) {
        this.password = password;
    }
    
}